#include "modifystuface.h"
#include "ui_modifystuface.h"

modifyStuface::modifyStuface(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::modifyStuface)
{
    ui->setupUi(this);

    appDir = QCoreApplication::applicationDirPath();
}

modifyStuface::~modifyStuface()
{
    delete ui;
}

void modifyStuface::on_sureBtn_clicked()
{
    QFile stufile(appDir + "/stuInfo.txt");
    if (stufile.exists() == false)
    {
        qDebug() << "创建stuInfo文件" << endl;
    }

    stufile.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (stufile.atEnd() == false) //文件没结束
    {
        QString line = stufile.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    stufile.close();

    // 查找并修改目标行
    bool found = false;
    for (int idx = 0; idx < lines.size(); ++idx)
    {
        QString data = lines[idx];
        if (lines[idx].split("+")[0] == ui->le_prevname->text() && lines[idx].split("+")[1] == ui->le_previd->text() && lines[idx].split("+")[2] == ui->le_prevpassword->text())
        {
            lines[idx] = ui->le_aftername->text() + "+" + ui->le_afterid->text() + "+" + ui->le_afterpassword->text() + "+" + lines[idx].split("+")[3].split("\n")[0] + "\n";
            found = true;
            modifyfile();
            break; // 只需要删除第一个匹配项
        }
    }

    if (!found) //没找到
    {
        QMessageBox::warning(this, "修改失败", "该信息有误");
        return;
    }
    else
    {
        QMessageBox::warning(this, "修改成功", "该信息已修改");
        ui->le_prevname->clear();
        ui->le_previd->clear();
        ui->le_prevpassword->clear();
        ui->le_aftername->clear();
        ui->le_afterid->clear();
        ui->le_afterpassword->clear();
        this->hide();
    }

    stufile.open(QIODevice::WriteOnly);

    QTextStream out(&stufile);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    stufile.close();

    emit updataView();
}

void modifyStuface::on_cancelBtn_clicked()
{
    ui->le_prevname->clear();
    ui->le_previd->clear();
    ui->le_prevpassword->clear();
    ui->le_aftername->clear();
    ui->le_afterid->clear();
    ui->le_afterpassword->clear();
    this->hide();
}

void modifyStuface::modifyfile()
{
    QFile prevfile(appDir + "/" + ui->le_prevname->text() + ".txt");
    if (prevfile.exists() == false)
    {
        qDebug() << "创建ui->le_prevname->text()文件" << endl;
    }

    prevfile.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (prevfile.atEnd() == false) //文件没结束
    {
        QString line = prevfile.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    prevfile.close();

    QFile afterfile(appDir + "/" + ui->le_aftername->text() + ".txt");
    if (afterfile.exists() == false)
    {
        qDebug() << "创建ui->le_aftername->text()文件" << endl;
    }

    afterfile.open(QIODevice::WriteOnly);

    QTextStream out(&afterfile);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    afterfile.close();

    prevfile.remove();
}
